<?php
/**
 * 会员
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-03-28
 * Time: 下午 11:26
 */
namespace App;

use Model\ExchangeMod;
use Model\MemberMod;
use Model\SettingMod;
use Pri\snapshot\Coupon;
use Pri\snapshot\CouponSnapshot;
use Pub\tool\Tool;
use Pub\wx\Pay;

class Member extends Action
{

	private $Member = null;
	private $Exchange = null;
	public function __construct()
	{
		parent::__construct();
		$this->Member = new MemberMod();
	}

	/**
	 * 注册
	 */
	public function register()
	{
        if(!$this->Member->findOne(array('uniacid'=>$this->_uniacid,'uid'=>$this->_gpc['uid']),array('id'))){
            $_result = $this->Member->insert(array(
                'uniacid'=>$this->_uniacid,
                'uid'=>$this->_gpc['uid']
            ));
            if($_result){
                $this->_result = array('status'=>'success');
            }
            $this->result($this->_result);
        }
        $this->_result = array('status'=>'success');
        $this->result($this->_result);
	}

    /**
     * 获取指定数据
     * @param uid 用户id
     */
	public function easyFind()
    {
        $_member = $this->Member->findOne(array('uniacid'=>$this->_uniacid,'uid'=>$this->_gpc['uid']),$this->_gpc['return']);
        if($_member) $this->_result = array('status'=>'success','data'=>$_member);
        $this->result($this->_result);
    }

    /**
     * 获取指定用户的头像和昵称
     * @param $_uid 用户的uid
     */
    public function memberInfo()
    {
        $_member = $this->uidToInfo($this->_gpc['uid']);
        $this->_result = array('status'=>'success','data'=>array(
            'nickname'=>$_member['nickname'],
            'header'=>$_member['avatar']
        ));
        $this->result($this->_result);
    }

	/**
	 * 提现
	 */
	public function deposit()
	{
		$_member = $this->Member->findOne(array('uniacid'=>$this->_uniacid,'uid'=>$this->_gpc['uid']),array('money','id'));
		$Setting = new SettingMod();
		$_setting = $Setting->findOne(array('uniacid'=>$this->_uniacid),array('deposit_limit','deposit_check','deposit_enter_detail'));
		if($_member['money'] < $_setting['deposit_limit']){
			$this->_result = array('status'=>'error','err_msg'=>'余额不足'.$_setting['deposit_limit'].'元','err_status'=>9);
			$this->result($this->_result);
		}
		$this->Member->update(array('money'=>0),array('id'=>$_member['id']));
        $this->Exchange = new ExchangeMod();
        $_insert_result = $this->Exchange->insert(array(
            'uniacid'=>$this->_uniacid,
            'uid'=>$this->_gpc['uid'],
            'money'=>$_member['money'],
            'time'=>TIMESTAMP
        ));
        $_insert_id = 0;
        if($_insert_result){
            $_insert_id = pdo_insertid();
            if($_setting['deposit_check'] > 0 && ($_member['money'] < $_setting['deposit_check'])){
                $_result = $this->depositWx($_member['money'],$_setting['deposit_enter_detail']);
                if($_result['status'] == 'success'){
                    $this->Exchange->update(array('status'=>1),array('id'=>$_insert_id));
                    $this->_result = array('status'=>'success','success_msg'=>'提现成功，已提现至微信钱包','data'=>TIMESTAMP);
                }else{
                    $this->Member->math(array('money +'=>$_member['money']),array('id'=>$_member['id']));
                    $this->Exchange->update(array('status'=>9),array('id'=>$_insert_id));
                    $this->_result = $_result;
                }
            }else{
                $this->_result = array('status'=>'success','success_msg'=>'申请提现成功，等待处理','data'=>TIMESTAMP);
            }
        }else{
            $this->Member->math(array('money +'=>$_member['money']),array('id'=>$_member['id']));
            $this->_result = array('status'=>'error','err_msg'=>'提现失败','err_status'=>8);
        }
		$this->result($this->_result);
	}

    /**
     * 微信提现
     * @param $_money
     * @param $_deposit_detail
     * @return bool|mixed|\Pub\wx\DOMDocument
     */
	private function depositWx($_money,$_deposit_detail)
    {
        $_fans = $this->uidToFans($this->_gpc['uid']);
        $Pay = new Pay(Tool::createOrder());
        return $Pay->payer($_fans['openid'],$_money,$_deposit_detail);
    }

	/**
	 * 会员总数
	 */
	public function total()
	{
		$_total = $this->Member->total(array('uniacid'=>$this->_uniacid));
		$this->_result = array('status'=>'success','data'=>$_total);
		$this->result($this->_result);
	}

	/**
	 * 后台会员列表
	 */
	public function li()
	{
		$_list = $this->Member->findAll(array('uniacid'=>$this->_uniacid),array('order'=>array('id','desc'),'limit'=>$this->_gpc['limit']),array('uid','money'));
		if(count($_list) > 0){
		    foreach($_list as $_k=>$_v){
                $_member = $this->uidToInfo($_v['uid']);
                $_list[$_k]['name'] = $_member['nickname'];
                $_list[$_k]['header'] = $_member['avatar'];
            }
			$this->_result = array('status'=>'success','data'=>$_list);
		}
		$this->result($this->_result);
	}

}